<template>
	<view class="chat-page-background">
		<view v-for="msg in msgList">
			<uni-dateformat :date="msg.createTime" format="yyyy-MM-dd hh:mm:ss" class="time-bar"></uni-dateformat>
			<view v-if="msg.receiverId != receiverId" class="msg-bar">
				<image :src="msg.senderLogo ? msg.senderLogo : '../../static/logo.png'" class="msg-logo" style="width: 100rpx;" @click="toHomePage(msg.senderId)"></image>
				<view class="send-msg-box">
					<view class="msg">{{msg.message}}</view>
				</view>
				<view class="msg-blank"></view>
			</view>
			<view v-else class="msg-bar">
				<view class="msg-blank"></view>
				<view class="receive-msg-box">
					<view class="msg">{{msg.message}}</view>
				</view>
				<image :src="msg.senderLogo ? msg.senderLogo : '../../static/logo.png'" class="msg-logo" style="width: 100rpx;"></image>
			</view>
		</view>
		<view class="page-bottom" style="background-color: #FFFFFF;"></view>
		<view class="chat-bottom">
			<input class="bottom-input" v-model="message" maxlength="500" />
			<view class="send-btn" @click="sendMsg">发送</view>
		</view>
	</view>
</template>

<script>
	import config from "@/config/app.js"
	export default {
		data() {
			return {
				id: "",
				message: "",
				receiverId: 0,
				receiverName: "",
				msgList: [],
				firstOpen: true
			};
		},
		methods: {
			getUser() {
				var that = this;
				this.$request.request({
					url: "/user/getUserToChat",
					method: "GET",
					data: {
						receiverId: that.receiverId
					}
				}).then(res=>{
					if (res.code == 200) {
						that.receiverName = res.data.receiverName;
						uni.setNavigationBarTitle({
							title: that.receiverName
						})
						that.getMesList();
					} else if(res.code == 401) {
						uni.showModal({
							title: res.message,
							showCancel: false,
							success(res) {
								if (res.confirm) {
									uni.navigateBack()
								}
							}
						})
					} else if (res.code == 405) {
						uni.showModal({
							title: res.message,
							showCancel: false,
							success(res) {
								if (res.confirm) {
									uni.navigateBack()
								}
							}
						})
					} else {
						uni.showModal({
							title: "发起聊天失败，请重试",
							showCancel: false,
							success(res) {
								if (res.confirm) {
									uni.navigateBack()
								}
							}
						})
					}
				})
			},
			sendMsg() {
				this.message = this.message.trim();
				if (this.message.length <= 0) {
					uni.showToast({
						title: "请输入有效的聊天信息",
						icon: "none"
					})
					return;
				}
				this.$request.request({
					url: "/chat/send",
					method: "POST",
					data: {
						receiverId: this.receiverId,
						message: this.message
					}
				}).then(res=>{
					if (res.code == 200) {
						this.getMesList();
						this.message = "";
					} else {
						uni.showToast({
							title: "发送失败",
							icon: "none"
						})
					}
				})
			},
			getMesList() {
				this.$request.request({
					url: "/chat/getList",
					method: "GET",
					data: {
						receiverId: this.receiverId
					}
				}).then(res=>{
					if (res.code == 200) {
						for (let i=0; i<res.data.length; i++) {
							if (res.data[i].receiverLogo) {
								res.data[i].receiverLogo = config.IMG_URL + res.data[i].receiverLogo;
							} else {
								res.data[i].receiverLogo = '../../static/logo.png'
							}
							if (res.data[i].senderLogo) {
								res.data[i].senderLogo = config.IMG_URL + res.data[i].senderLogo;
							} else {
								res.data[i].senderLogo = '../../static/logo.png'
							}
						}
						this.msgList = res.data;
						this.$nextTick(function(){
							if (this.firstOpen) {
								uni.pageScrollTo({
										scrollTop: 99999999999
								})
								this.firstOpen = false
							}
						})
					}
				})
			},
			toHomePage(id) {
				uni.navigateTo({
					url: "../user/homePage?userId=" + id
				})
			},
		},
		mounted() {
		  this.timer = setInterval(this.getMesList, 1000);
		},
		beforeDestroy() {
		  clearInterval(this.timer);
		},
		onLoad(option) {
			this.receiverId = option.receiverId;
			this.getUser();
			// uni.pageScrollTo({
			//   scrollTop: 2000000,    //滚动到页面的目标位置（单位px）
			//   duration: 0    //滚动动画的时长，默认300ms，单位 ms
			// });
		},
		onPullDownRefresh() {
			this.getMesList();
			uni.stopPullDownRefresh();
		},
	}
</script>

<style lang="scss">

	.chat-page-background {
		background-color: #FFFFFF;
		padding-top: 40rpx;
		
		.time-bar {
			width: 100%;
			font-size: 14px;
			color: #999999;
			text-align: center;
			display: block;
		}
		
		.msg-bar {
			display: flex;
			padding: 20rpx;
			
			.msg-logo {
				width: 100prx;
				height: 100rpx;
				border-radius: 50rpx;
			}
			
			.send-msg-box {
				flex: 1;
				display: flex;
				align-items: center;
				justify-content: flex-start;
				
				.msg {
					margin-left: 20rpx;
					font-size: 30rpx;
					line-height: 44rpx;
					padding: 20rpx;
					background-color: #007AFF;
					color: #FFFFFF;
					border-radius: 20rpx;
					text-align: justify;
				}
			}
			
			.receive-msg-box {
				flex: 1;
				display: flex;
				align-items: center;
				justify-content: flex-end;
				
				.msg {
					margin-right: 20rpx;
					font-size: 30rpx;
					line-height: 44rpx;
					padding: 20rpx;
					background-color: #007AFF;
					color: #FFFFFF;
					border-radius: 20rpx;
					text-align: justify;
				}
			}
			
			
			.msg-blank {
				width: 100rpx;
			}
			
		}
		
		
		.chat-bottom {
			position: fixed;
			width: 100%;
			bottom: 0;
			display: inline-flex;
			background-color: #FFFFFF;
			
			.bottom-input {
				margin: 30rpx 30rpx;
				width: 520rpx;
				border-bottom: 1rpx #999999 solid;
			}
			
			.send-btn {
				margin: 20rpx 0;
				line-height: 60rpx;
				width: 140rpx;
				text-align: center;
				border-radius: 20rpx;
				background-color: #11A8CD;
				color: #FFFFFF;
			}
		}
		
	}
</style>
